<?php
/**
 * 基础物料控制层
 *
 * @author sunjing@shopex.cn
 * @version 0.1
 */

class material_ctl_admin_material_giftreplace extends desktop_controller{


    /**
     * 基础物料列表项方法
     *
     * @param Post
     * @return String
     */
    function index(){

        $use_buildin_export    = false;
        $use_buildin_import    = false;
        $actions               = array();
        
        $actions    = array(
                            array(
                                'label' => '新增赠品物料替换申请',
                                'href' => 'index.php?app=material&ctl=admin_material_giftreplace&act=add',
                                'target' => '_blank',
                            ),
                        );
        
        $params = array(
            'title'=>'赠品物料替换申请与审批',
            'actions' => $actions,
            'base_filter' => $base_filter,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_filter'=>true,
            'use_buildin_recycle' => true,
            'use_buildin_export'=> $use_buildin_export,
            'use_buildin_import'=> $use_buildin_import,
            'finder_cols' => 'material_name,material_bn,rep_material_bn,rep_material_name,num,replaceNum,priority',

        );
        $this->finder('material_mdl_basic_giftreplace',$params);
    }

    function add(){        
        //material_bn
        $shopObj = app::get('ome')->model('shop');
        $shopList = $shopObj->getList('shop_id,name', array('s_type'=>1), 0, -1);
        $start_time = time();
        $end_time= time()+86400;
        $this->pagedata['shops'] = $shopList;
        $this->pagedata['start_time'] = $start_time;
        $this->pagedata['end_time'] = $end_time;
        $this->singlepage('admin/material/giftreplace/add.html');
    }


    function toAdd(){
        $this->begin();

        $shopdata = isset($_POST['shop_id']) ? implode(",", $_POST['shop_id']) : '';
        $materialMdl = app::get('material')->model('basic_material');
        $basicrepMdl = app::get('material')->model('basic_giftreplace');

        $start_time = strtotime($_POST['start_time'] . ' ' . $_POST['_DTIME_']['H']['start_time'] . ':' . $_POST['_DTIME_']['M']['start_time']);
        $end_time = strtotime($_POST['end_time'] . ' ' . $_POST['_DTIME_']['H']['end_time'] . ':' . $_POST['_DTIME_']['M']['end_time']);

        //$bm_id = $materialMdl->getList('bm_id',array('material_bn' => $_POST['material_bn']));
        $bm_id = $_POST['bm_id'];
       

        if (!$bm_id) {
            $this->end(false,'原料品在系统中不存在');
        }

        
        if($start_time >= $end_time){
            $this->end(false,'开始时间不能大于等于结束时间');
        }
                   
        $material_detail = $materialMdl->getList('material_bn,material_name,bm_id',array('bm_id'=>$bm_id));
      
        $data = array(
            'bm_id' => $bm_id,
            'material_bn' => $material_detail[0]['material_bn'],
            'material_name' => $material_detail[0]['material_name'],
          
            'priority' => $_POST['priority'],
            'shopIds' => $shopdata,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'disabled' => $_POST['disabled'],
            'desc' => $_POST['desc'],
        );
        $result = $basicrepMdl->save($data);

        if($result){
            $this->end(true,'赠品物料保存成功');
        }else{
            $this->end(false,'赠品物料保存失败');
        }

    }

    function edit($rep_id){
        
        $shopObj = app::get('ome')->model('shop');
        $shopList = $shopObj->getList('shop_id,name', array('s_type'=>1), 0, -1);
        $replaceMdl = app::get('material')->model('basic_giftreplace');
        $replaceInfo = $replaceMdl->getList('*',array('rep_id'=>$rep_id));
        $shopdata = explode(',',$replaceInfo[0]['shopIds']);

        $this->pagedata['shops'] = $shopList;
        $this->pagedata['replaceInfo'] = $replaceInfo;
        $this->pagedata['shopdata'] = $shopdata;
        $this->singlepage('admin/material/giftreplace/edit.html');
        
    }


    function toEdit($rep_id){
        $this->begin();
        $shopdata = isset($_POST['shop_id']) ? implode(",", $_POST['shop_id']) : '';
        $materialMdl = app::get('material')->model('basic_material');
        $basicrepMdl = app::get('material')->model('basic_giftreplace');

        $start_time = strtotime($_POST['start_time'] . ' ' . $_POST['_DTIME_']['H']['start_time'] . ':' . $_POST['_DTIME_']['M']['start_time']);
        $end_time = strtotime($_POST['end_time'] . ' ' . $_POST['_DTIME_']['H']['end_time'] . ':' . $_POST['_DTIME_']['M']['end_time']);

        $bm_id = $_POST['bm_id'];
       

        if (!$bm_id) {
            $this->end(false,'原料品在系统中不存在');
        }

        if($start_time >= $end_time){
            $this->end(false,'开始时间不能大于等于结束时间');
        }

        $material_detail = $materialMdl->getList('material_bn,material_name,bm_id',array('bm_id'=>$bm_id));
        

        $data = array(
            'bm_id' => $bm_id,
            'material_bn' => $material_detail[0]['material_bn'],
            'material_name' => $material_detail[0]['material_name'],
           
            'priority' => $_POST['priority'],
            'shopIds' => $shopdata,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'disabled' => $_POST['disabled'],
            'desc' => $_POST['desc'],
        );
        $result = $basicrepMdl->update($data,array('rep_id'=>$_POST['rep_id']));

        if($result){
            $this->end(true,'物料替换保存成功');
        }else{
            $this->end(false,'物料替换保存失败');
        }
        
    }


    function do_check($rep_id){
        $this->begin('javascript:finderGroup["'.$_GET['finder_id'].'"].refresh();');
        $basicrepMdl = app::get('material')->model('basic_giftreplace');

        $basicrepMdl->update(array('check_status'=>'2'),array('rep_id'=>$rep_id,'check_status'=>'1'));
        
         $this->end(true,'确认成功!');
    }

    function do_update($rep_id){
     
        $this->begin('javascript:finderGroup["'.$_GET['finder_id'].'"].refresh();');
        $basicrepMdl = app::get('material')->model('basic_giftreplace');
        if (in_array($_GET['oper'],array('close','open'))){
            if ($_GET['oper'] == 'open'){
                $basicrepMdl->update(array('disabled'=>'true'),array('rep_id'=>$rep_id,'disabled'=>'false'));
            }else{
                $basicrepMdl->update(array('disabled'=>'false'),array('rep_id'=>$rep_id,'disabled'=>'true'));
            }
            
        }
        
        $this->end(true,'操作成功!');
    }
}
